const { DataTypes } = require('sequelize');
const sequelize = require('../../config/database');

const StudentActivity = sequelize.define('student_activity', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    allowNull: false
  },
  student_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '学生ID',
    references: {
      model: 'students',
      key: 'id'
    }
  },
  activity_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '活动ID',
    references: {
      model: 'activities',
      key: 'id'
    }
  },
  status: {
    type: DataTypes.ENUM('registered', 'attended', 'missed'),
    defaultValue: 'registered',
    allowNull: false,
    comment: '报名状态'
  },
  points_awarded: {
    type: DataTypes.DECIMAL(10, 2),
    defaultValue: 0.00,
    allowNull: true,
    comment: '该活动实际获得的积分'
  },
  register_time: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW,
    allowNull: false,
    comment: '报名时间'
  },
  attend_time: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '签到/出席时间'
  }
}, {
  tableName: 'student_activity',
  timestamps: false,
  charset: 'utf8mb4',
  collate: 'utf8mb4_general_ci',
  comment: '学生报名活动记录',
  indexes: [
    {
      unique: true,
      fields: ['student_id', 'activity_id'],
      name: 'unique_student_activity'
    }
  ]


  
});


StudentActivity.associate = (models) => {
  StudentActivity.belongsTo(models.Activities, {
    foreignKey: 'activity_id',
    as: 'activity' // 这个别名用于查询
  });
};
module.exports = StudentActivity;
